package CryptoPrim;

import ArithmeticObjects.*;
import Utils.ByteTree;

/**
 * A class which represents a public key (pk).
 * 
 * @author Itamar Carmel, nirhagin
 */
public class PubKey {

	// suppose pk = (g,y)
	private VerfGroup Gq; // Group of pk's elements
	private VerfGroup.VElement g; // first of the pair - g
	private VerfGroup.VElement y; // second of the pair - y
	
	/**
	 * Constructor of Public Key from group and two elements.
	 * 
	 * @param Gq
	 * @param g
	 * @param y
	 */
	public PubKey(VerfGroup Gq, VerfGroup.VElement g, VerfGroup.VElement y) {
		this.Gq = Gq;
		this.g = g;
		this.y = y;
	}
	
	/**
	 * Convert Public Key to it's BT rep. 
	 * 
	 * @return BT(PK)
	 */
	public ByteTree getByteTreeRep(int width){
		ByteTree ret = ByteTree.generateEmptyNode();
		if (width == 1){
			ret.addMultChildren(
					Gq.toByteTree(this.g),
					Gq.toByteTree(this.y));
		} else {
			ByteTree gs = ByteTree.generateEmptyNode();
			ByteTree ys = ByteTree.generateEmptyNode();
			for (int i=0; i<width; i++){
				gs.addChild(Gq.toByteTree(this.g));
				ys.addChild(Gq.toByteTree(this.y));
			}
			ret.addMultChildren(gs,ys);
		}
		return ret;
	}
	
	/**
	 * Convert BT to it's PK rep.
	 * 
	 * @param bt
	 * @param Gq
	 * @return PK(bt,Gq)
	 */
	public static PubKey getPubKey(ByteTree bt, VerfGroup Gq){
		return new PubKey(
				Gq, Gq.getElement(bt.getTheNChild(0)), Gq.getElement(bt.getTheNChild(1)));
	}

	/**
	 * Getter for y.
	 * 
	 * @return y
	 */
	public VerfGroup.VElement getY() {
		return this.y;
	}

	/**
	 * Getter for g.
	 * 
	 * @return g
	 */
	public VerfGroup.VElement getG() {
		return this.g;
	}
	
}
